﻿Imports System.IO

Public Class FormularioLogeo
    Dim usuarioAccediendo As Empleado

    Dim dia As String = "c:/eleemeTPV/caja/" & Today.Day & Today.Month & Today.Year & ".txt"
    Dim mes As String = "c:/eleemeTPV/caja/" & Today.Year & Today.Month & ".txt"
    Dim anho As String = "c:/eleemeTPV/caja/" & Today.Year & ".txt"
    Friend Function getUsuarioAccediendo() As Empleado
        Return usuarioAccediendo
    End Function
    Private Sub FormularioLogeo_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Creación de archivos de caja diaria, mensual y anual si no existen
        comprobarArchivosCaja()
    End Sub

    Private Sub comprobarArchivosCaja()
        Dim cajaDia As FileStream = New FileStream(dia, FileMode.OpenOrCreate, FileAccess.Read)
        Dim cajaMes As FileStream = New FileStream(mes, FileMode.OpenOrCreate, FileAccess.Read)
        Dim cajaAnho As FileStream = New FileStream(anho, FileMode.OpenOrCreate, FileAccess.Read)
        Try
            Dim sr As New StreamReader(cajaDia)
            If sr.ReadToEnd().Length = 0 Then
                sr.Close()
                cajaDia.Close()
                cajaDia = New FileStream(dia, FileMode.Open, FileAccess.Write)
                Dim sw As New StreamWriter(cajaDia)
                sw.Write(0)
                sw.Close()
            End If
            sr = New StreamReader(cajaMes)
            If sr.ReadToEnd().Length = 0 Then
                sr.Close()
                cajaMes.Close()
                cajaMes = New FileStream(mes, FileMode.Open, FileAccess.Write)
                Dim sw As New StreamWriter(cajaMes)
                sw.Write(0)
                sw.Close()
            End If
            sr = New StreamReader(cajaAnho)
            If sr.ReadToEnd().Length = 0 Then
                sr.Close()
                cajaAnho.Close()
                cajaAnho = New FileStream(anho, FileMode.Open, FileAccess.Write)
                Dim sw As New StreamWriter(cajaAnho)
                sw.Write(0)
                sw.Close()
            End If
            cajaAnho.Close()
            cajaMes.Close()
            cajaDia.Close()

        Catch ex As Exception
            MsgBox("Algo malo ocurrió")
        End Try
    End Sub

    Private Sub butAcceder_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butAcceder.Click
        'buscarNombre usuario en el archivo
        usuarioAccediendo = buscarEmpleado(txbUsuario.Text)
        If usuarioAccediendo Is Nothing Then
            MsgBox("El usuario no existe.")
            txbUsuario.Text = ""
        Else
            'habría que chequear que la pass es correcta
            If verificarPassword(txbPassword.Text) Then
                Sesion.setEmpleadoActivo(usuarioAccediendo)
                Me.Visible = False
                FormularioTratamientos.Visible = True
            Else
                MsgBox("La contraseña no es correcta. Por favor, inténtelo de nuevo.")
                txbPassword.Clear()
            End If
        End If
    End Sub

End Class
